#include "RemoteController.h"
#include <SFML/Network.hpp>
#include <iostream>

cat::RemoteController::RemoteController(Tank& tank, sf::TcpSocket* socket)
	: cat::RemoteController::ControllerBase(tank),
	_socket(socket)
{
}


cat::RemoteController::~RemoteController(void)
{
}

void cat::RemoteController::treatEvent(sf::Event& event)
{
}

void cat::RemoteController::think(void)
{
	sf::Packet packet;
	_socket->receive(packet);

	if(packet.getDataSize() > 0)
	{
		double x, y, vx, vy, cx, cy;
		packet >> x 
			>> y
			>> vx
			>> vy
			>> cx
			>> cy;

		std::cout 
			<< "Received location: ("<< x << "," << y << ")" << std::endl 
			<< "Received velocity: (" << vx << "," << vy << ")" << std::endl << std::endl;

		_tank.update(x, y, vx, vy);
		_controlPosition.x = cx;
		_controlPosition.y = cy;
	}

	if(_controlPosition.x == 0 && _controlPosition.y == 0)
	{
		_tank.setVelocity(0);
	}
	else
	{
		double angle = atan2(_controlPosition.y, _controlPosition.x);
		double velocity = 1;

		_tank.move(angle, velocity);
	}
}